<template>
  <el-dialog
    title="派发卡劵"
    :visible.sync="dialogVisible"
    width="50%"
    class="common-dialog"
  >
    <el-form
      ref="form"
      :model="form"
      label-width="150px"
    >
      <el-form-item
        label="卡劵"
        prop="t_id"
        :rules="[{required: true, message: '请选择卡劵'}]"
      >
        <el-select
          size="small"
          v-model="form.t_id"
          filterable
          placeholder="请选择卡劵"
        >
          <el-option label="请选择" value=""></el-option>
          <el-option
            v-for="item in couponList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label=" ">{{firstUser}}</el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        size="small"
        @click="dialogVisible = false"
      >
        取 消
      </el-button>
      <el-button
        size="small"
        type="primary"
        :loading="submit_loading"
        @click="ensureDispatch"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-29
   * @Project: driver
   */
  import {mapState} from 'vuex'

  export default {
    name: "dispatch"
    , data() {
      return {
        dialogVisible: false
        , users: []
        , form: {
          t_id: ''
        }
        , submit_loading: false
      }
    }
    , computed: {
      ...mapState([
        'couponList'
      ])
      , firstUser() {
        let users = this.users
          , len = users.length
        if (len === 0) return ''
        return '正在为 ' + users[0].nickName + (len > 1 ? ' 等' + len + '名用户' : '') + ' 派发卡劵'
      }
    }
    , watch: {
      dialogVisible(bool) {
        this.$fn.setTitle(bool ? '派发卡劵' : '用户管理')
      }
    }
    , mounted() {
      this.init()
    }
    , methods: {
      init() {
        this.$bus.$on('coupon-dispatch', users => {
          this.dialogVisible = true
          this.users = users
          this.form.t_id = ''
        })
      }
      , ensureDispatch() {
        this.$refs.form.validate(valid => {
          if (!valid) return
          const {t_id} = this.form
          const u_id = [],
            phone = []
          this.users.forEach(user => {
            u_id.push(user.id)
            phone.push(user.phone)
          })
          this.submit_loading = true
          this.$axios.post('/api/v1/ticket/send', {
              t_id,
              u_id: u_id.join(','),
              phone: phone.join(',')
            })
            .then(res => {
              if (res.success) {
                this.$message({
                  message: '派送成功'
                  , type: 'success'
                })
                this.dialogVisible = false
                this.$bus.$emit('user-clear')
              } else {
                this.$message({
                  message: '派送失败' + res.msg
                  , type: 'error'
                })
              }
            })
            .catch(() => {
              this.$message({
                message: '派送失败'
                , type: 'error'
              })
            })
            .finally(() => {
              this.submit_loading = false
            })
        })
      }
    }
  }
</script>

<style scoped>

</style>
